//
//  CommunityTableCellOfGroup.m
//  aMoment
//
//  Created by dllo on 15/10/11.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "CommunityTableCellOfGroup.h"
#import "AllHeaderFiles.h"

@implementation CommunityTableCellOfGroup

//- (void)dealloc
//{
//    [_coverImageView release];
//    [_titleLabel release];
//    [_tagField release];
//    [_contentImageViewArray release];
//    [_contentImageView1 release];
//    [_contentImageView2 release];
//    [_contentImageView3 release];
//    [_contentImageView4 release];
//    [_usernameLabel release];
//    [_addTimeLabel release];
//    [super dealloc];
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)createViews
{
    _coverImageView = [[UIImageView alloc] init];
//    _coverImageView.backgroundColor = [UIColor grayColor];
    _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    _coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:_coverImageView];
    [_coverImageView release];
    
    _titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    
    _tagField = [[UITextField alloc] init];
    _tagField.enabled = NO;
    _tagField.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _tagField.textColor = FONTCOLOR_GRAY;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
    imageView.image = [UIImage imageNamed:@"iconfont-tag"];
    _tagField.leftView = imageView;
    _tagField.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:_tagField];
    [_tagField release];
    
    
    
    
    
    
    self.contentImageViewArray = [NSArray arrayWithObjects:_groupImageView1, _groupImageView2, _groupImageView3, _groupImageView4, nil];
    for (NSInteger i = 0; i < 4; i++) {
        GroupLabelAndImageView *imageView = [[GroupLabelAndImageView alloc] init];
        imageView.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.coverImageView.clipsToBounds = YES;
        imageView.tag = 1000 + i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageAction:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
//        [tap release];
        
        [self.contentView addSubview:imageView];
        if (i == 0) {
            _groupImageView1 = imageView;
        } else if (i == 1) {
            _groupImageView2 = imageView;
        } else if (i == 2) {
            _groupImageView3 = imageView;
        } else {
            _groupImageView4 = imageView;
        }
        [imageView release];
    }
    
    
    
    _usernameLabel = [[UILabel alloc] init];
    _usernameLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _usernameLabel.textColor = FONTCOLOR_GRAY;
    [self.contentView addSubview:_usernameLabel];
    [_usernameLabel release];
    
    _addTimeLabel = [[UILabel alloc] init];
    _addTimeLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _addTimeLabel.textColor = FONTCOLOR_GRAY;
    _addTimeLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_addTimeLabel];
    [_addTimeLabel release];
}

#pragma mark 点击图片
- (void)clickImageAction:(UITapGestureRecognizer *)tap
{
//    NSLog(@"%ld", tap.view.tag);
//    NSLog(@"cell%ld", _cellTag);
    [self.delegate passImageViewTag:tap.view.tag CellTag:_cellTag];
}
/*-----------------------------------------*/
- (void)layoutSubviews
{
    [super layoutSubviews];
    _coverImageView.frame = CGRectMake(20 * FIT_WIDTH, 15 * FIT_HEIGHT, 45 * FIT_WIDTH, 45 * FIT_WIDTH);
    _coverImageView.layer.cornerRadius = _coverImageView.Width / 2;
    _titleLabel.frame = CGRectMake(_coverImageView.X + _coverImageView.Width + 5 * FIT_WIDTH, _coverImageView.Y, SCREEN_WIDTH - _coverImageView.X * 2 - _coverImageView.Width - 5 * FIT_WIDTH, _coverImageView.Height / 2);
    _tagField.frame = CGRectMake(_titleLabel.X, _titleLabel.Y + _titleLabel.Height, _titleLabel.Width, _titleLabel.Height);
    
    self.contentImageViewArray = [NSArray arrayWithObjects:_groupImageView1, _groupImageView2, _groupImageView3, _groupImageView4, nil];
    for (NSInteger i = 0; i < 4; i++) {
        [[_contentImageViewArray objectAtIndex:i] setFrame:CGRectMake(_coverImageView.X + (80 + 5) * FIT_WIDTH * i, _coverImageView.Y + _coverImageView.Height + 15 * FIT_HEIGHT, 80 * FIT_WIDTH, 80 * FIT_WIDTH)];
        
    }
    
    _usernameLabel.frame = CGRectMake(_groupImageView1.X, _groupImageView1.Y + _groupImageView1.Height + 15 * FIT_HEIGHT, (SCREEN_WIDTH - 40 * FIT_WIDTH) / 2, 20 * FIT_HEIGHT);
    _addTimeLabel.frame = CGRectMake(_usernameLabel.X + _usernameLabel.Width, _usernameLabel.Y, _usernameLabel.Width, _usernameLabel.Height);
}

#pragma mark 重写set
- (void)setCommunity:(Community *)community
{
    if (_community != community) {
        [_community release];
        _community = [community retain];
    }
//    [_coverImageView setImageWithURL:[NSURL URLWithString:_community.coverimg]];
    [_coverImageView setImageWithURL:[NSURL URLWithString:_community.coverimg] placeholderImage:[UIImage imageNamed:@"iconfont-head@2x"]];
    _titleLabel.text = _community.title;
    _tagField.text = [NSString stringWithFormat:@" %@", _community.tags[0]];
    
    if (_community.tags.count > 1) {
        for (NSInteger i = 1; i < _community.tags.count; i++) {
            _tagField.text = [NSString stringWithFormat:@"%@/%@", _tagField.text, _community.tags[i]];
        }
    }
    
    //设置tag
    self.contentView.tag = _cellTag;
    
    self.contentImageViewArray = [NSArray arrayWithObjects:_groupImageView1, _groupImageView2, _groupImageView3, _groupImageView4, nil];
    for (NSInteger i = 0; i < 4; i++) {
        NSString *titleString = [NSString string];
        NSString *contentString = [NSString string];
        if ([[_community.latestposts[i] objectForKey:@"coverimg"] isEqualToString:@""]) {
            [[_contentImageViewArray[i] coverImageView] setImage:[UIImage imageNamed:@"iconfont-square"]];
            titleString = [_community.latestposts[i] objectForKey:@"title"];
            contentString = [_community.latestposts[i] objectForKey:@"content"];
        } else {
            [[_contentImageViewArray[i] coverImageView] setImageWithURL:[NSURL URLWithString:[_community.latestposts[i] objectForKey:@"coverimg"]] placeholderImage:[UIImage imageNamed:@"iconfont-square"]];
            titleString = @"";
            contentString = @"";
        }
        [[_contentImageViewArray[i] titleLabel] setText:titleString];
        [[_contentImageViewArray[i] contentLabel] setText:contentString];
    }
    
    _usernameLabel.text = [NSString stringWithFormat:@"组长: %@", [_community.userinfo objectForKey:@"uname"]];
    _addTimeLabel.text = _community.lastupdate_f;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
